//
// Created by SDSC\202100300261 on 23-11-16.
//

#include "CodeTable.h"
std::ostream& operator<<(std::ostream& os, const OPTS& value) {
    switch (value) {
        case OPTS::LIT:
            os << "LIT";
            break;
        case OPTS::LOD:
            os << "LOD";
            break;
        case OPTS::STO:
            os << "STO";
            break;
        case OPTS::CAL:
            os << "CAL";
            break;
        case OPTS::INT:
            os << "INT";
            break;
        case OPTS::JMP:
            os << "JMP";
            break;
        case OPTS::JPC:
            os << "JPC";
            break;
        case OPTS::OPR:
            os << "OPR";
            break;
        default:
            os << "unknown";
            break;
    }
    return os;
}

CodeEntry::CodeEntry(OPTS _opt, int _level, int _adr,int _index) {
    opt = _opt;
    level = _level;
    adr = _adr;
    index = _index;
}
std::ostream& operator<<(std::ostream& os,const CodeEntry& obj) {
//    os << obj.index<<": (" << obj.opt << "," << obj.level << "," << obj.adr << ")"<<std::endl;
    os<<obj.index<<" "<<obj.opt<<" "<<obj.level<<" "<<obj.adr<<std::endl;
    return os;
}
std::ostream& operator<<(std::ostream& os,const CodeTable& obj) {
    for(auto i : obj.codeTable) {
        os << i;
    }

    return os;
}

int CodeTable::addCode(OPTS opt, int level, int adr){
    codeTable.push_back(CodeEntry(opt,level,adr,codeTable.size()));
    return codeTable.size() - 1;
}

int CodeTable::getIndex() {
    return codeTable.size() - 1;
}
void CodeTable::setCode(int index, OPTS opt, int level, int adr) {
    codeTable[index] = CodeEntry(opt,level,adr,index);
}